package org.testtools.connector.impl;

import java.net.Socket;

import org.testtools.connector.Connector;

public class SocketConnector implements Connector {

  private String host = null;

  private int port = 0;

  public SocketConnector() {
  }

  public SocketConnector(String host, int port) {
    this.host = host;
    this.port = port;
  }

  public static boolean testConnection(String host, int port) {
    return new SocketConnector(host, port).testConnection();
  }

//  @Override
  public Socket getConnection() throws Exception {
    final Socket socket = new Socket(host, port);
    socket.setKeepAlive(true);

    return socket;
  }

//  @Override
  public boolean testConnection() {
    try {
      final Socket socket = getConnection();

      socket.getInputStream();
      socket.getOutputStream();
      socket.close();

      return true;
    }
    catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
}
